﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MossmanMill.Controller;
using System.Diagnostics;
using System.IO;

namespace MossmanMill.View
{
    /// <summary>
    /// MCM PDF Report Form class that create a PDF report from MCM database.
    /// </summary>
    /// <remarks>
    /// This class is thread safe. 
    /// </remarks>
    /// <dates>
    /// 17/05/2012 - created by Chiaki Sato
    /// </dates>
    public partial class ReportForm : Form
    {

        private const string REPORT_FILE_NAME = "MCM Rack Report.pdf";
        
        // Messages
        private const string MESSAGE_REPORT_SUCCESS = "MCM PDF Report was successfully created.";

        // Error Messages
        private const string ERROR_MESSAGE_REPORT_IOEXCEPTION = "Report Creation Error, MCM PDF Report is currently used or opened.\nPlease close it first before creating new MCM PDF Report.";

        /// <summary>
        /// Constructor for ReportForm class. 
        /// </summary>
        public ReportForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Create report button click event hander. 
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event arguments</param>
        private void ButtonCreateReport_Click(object sender, EventArgs e)
        {
            try
            {
                // Create Report Manager by Factory
                IReportFactory reportFactory = new ReportFactory();
                IReportManager reportManager = reportFactory.Make(ReportFactory.CLASS_TYPE_REPORT, REPORT_FILE_NAME);

                this.Cursor = Cursors.WaitCursor;

                // Create PDF Report
                reportManager.CreateReport();

                if (checkBoxShowReport.Checked == true)
                {
                    Process.Start(REPORT_FILE_NAME);
                }
                else
                {
                    MessageBox.Show(MESSAGE_REPORT_SUCCESS, "MCM Report", MessageBoxButtons.OK);
                }

            }
            catch (IOException exception)
            {
                MessageBox.Show(ERROR_MESSAGE_REPORT_IOEXCEPTION, "MCM Report", MessageBoxButtons.OK);
                IOException dumy = exception;

            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString(), "MCM Report", MessageBoxButtons.OK);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Close button click event hander. 
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event arguments</param>
        private void ButtonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
